package com.cosmic.pizza.web.servlet;

import com.cosmic.pizza.pojo.Food;
import com.cosmic.pizza.pojo.OrderInfo;
import com.cosmic.pizza.service.FoodService;
import com.cosmic.pizza.service.Impl.FoodServiceImpl;
import com.cosmic.pizza.service.Impl.OrderServiceImpl;
import com.cosmic.pizza.service.OrderService;
import com.cosmic.pizza.util.SessionGetListUtils;
import com.cosmic.pizza.web.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/CartServlet/*")
public class CartServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();
    FoodService foodService = new FoodServiceImpl();
    public void AddToCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        //从session里获得购物车list
        List<OrderInfo> cart = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
        /*List<OrderInfo> cart = (List<OrderInfo>) req.getSession().getAttribute("cart");*/

        //创建自己的购物车list
        List<OrderInfo> myCart = new ArrayList<OrderInfo>();

        //往cart里添加订单详情 start
        OrderInfo orderInfo = new OrderInfo();
        Integer fid = Integer.valueOf(req.getParameter("fid"));
        orderInfo.setFid(fid);
        Food food = foodService.findByFid(fid);
        orderInfo.setFood(food);
        String size = req.getParameter("size");
        orderInfo.setSize(size);
        Double singlePrice = Double.valueOf(req.getParameter("singlePrice"));
        orderInfo.getFood().setPrice(singlePrice);
        Integer sum = Integer.valueOf(req.getParameter("sum"));
        orderInfo.setSum(sum);

        // end

        //将cart存回session
         if (cart==null){
             orderInfo.setCart_id(0);
             myCart.add(0,orderInfo);
             req.getSession().setAttribute("cart",myCart);
         }else {
             orderInfo.setCart_id(cart.size());
             cart.add(cart.size(),orderInfo);
             req.getSession().setAttribute("cart",cart);
         }

        List<OrderInfo> test = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
         for (OrderInfo orderInfo1 : test){
             System.out.println(orderInfo1.toString());
         }
        writeValue(resp,orderInfo);
    }
    public void GetCartCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<OrderInfo> cart = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
        int count = 0;
        if (cart!=null){
            count = cart.size();
        }
        writeValue(resp,count);
    }
    public void GetCartInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<OrderInfo> cart = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
        writeValue(resp, cart);
    }
    public void CartAddNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<OrderInfo> cart = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
        Integer cart_id = Integer.valueOf(req.getParameter("cart_id"));
        for (OrderInfo orderInfo : cart){
            if (orderInfo.getCart_id().equals(cart_id)){
                orderInfo.setSum(orderInfo.getSum()+1);
            }
        }
        req.getSession().setAttribute("cart",cart);
        writeValue(resp,cart);
    }
    public void CartCutNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<OrderInfo> cart = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
        Integer cart_id = Integer.valueOf(req.getParameter("cart_id"));
        for (OrderInfo orderInfo : cart){
            if (orderInfo.getCart_id().equals(cart_id)){
                if (orderInfo.getSum()>0) {
                    orderInfo.setSum(orderInfo.getSum() - 1);
                }
            }
        }
        req.getSession().setAttribute("cart",cart);
        writeValue(resp,cart);
    }
    public void DeleteCartInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<OrderInfo> cart = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
        Integer cart_id = Integer.valueOf(req.getParameter("cart_id"));
        cart.removeIf(orderInfo -> orderInfo.getCart_id().equals(cart_id));
        req.getSession().setAttribute("cart",cart);
        writeValue(resp,cart);
    }
}
